package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.mapper.CheckGroupMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {
    @Autowired
    private CheckGroupMapper checkGroupMapper;

    //分页查询检查组功能
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //获取前端传回的页码和每页显示条数信息
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        //获取查询条件
        String queryString = queryPageBean.getQueryString();
        //调用分页插件分页
        PageHelper.startPage(currentPage, pageSize);
        //调用持久层的方法
        Page<CheckGroup> page = checkGroupMapper.findPage(queryString);
        //获取数据集合和总数据数量
        List<CheckGroup> list = page.getResult();
        long total = page.getTotal();

        return new PageResult(total, list);
    }

    //业务层新增检查组功能
    @Override
    public void add(CheckGroup checkGroup, Integer[] checkitemIds) {
        //1.先向检查组表中添加页面返回的检查组数据,并返回添加成功后的检查组id
        checkGroupMapper.add(checkGroup);
        setCheckGroupAndCheckItem(checkGroup, checkitemIds);
        //2.使用添加成功后返回的检查组id,关联该检查组中的检查项
 /*       Integer id = checkGroup.getId();

        Map<String, Integer> map = new HashMap<>();
        //遍历数组
        if (checkitemIds != null && checkitemIds.length > 0) {
            for (Integer checkitemId : checkitemIds) {
                map.put("checkitemId", checkitemId);
                map.put("checkgroupId", id);
                checkGroupMapper.setCheckGroupAndCheckItem(map);
            }
        }*/
    }

    //根据id查询检查组功能
    @Override
    public CheckGroup findById(Integer checkgroupId) {
        return checkGroupMapper.findById(checkgroupId);
    }

    //查询检查组关联的检查项
    @Override
    public List<Integer> findCheckitemIdsByCheckgroupId(Integer checkgroupId) {
        return checkGroupMapper.findCheckitemIdsByCheckgroupId(checkgroupId);
    }


    //建立检查组和检查项多对多关系
    public void setCheckGroupAndCheckItem(CheckGroup checkGroup, Integer[] checkitemIds) {
        Integer id = checkGroup.getId();
        Map<String, Integer> map = new HashMap<>();
        //遍历数组
        if (checkitemIds != null && checkitemIds.length > 0) {
            for (Integer checkitemId : checkitemIds) {
                map.put("checkitemId", checkitemId);
                map.put("checkgroupId", id);
                checkGroupMapper.setCheckGroupAndCheckItem(map);
            }
        }
    }

    //编辑检查组功能
    @Override
    public void edit(CheckGroup checkGroup, Integer[] checkitemIds) {
        //1.编辑后的检查组的数据进行后端更改
        checkGroupMapper.edit(checkGroup);

        //2.删除检查组关联的检查项信息
        checkGroupMapper.deleteCheckGroupAndCheckItem(checkGroup.getId());

        //3.重新建立修改后的检查组与检查项的数据
        setCheckGroupAndCheckItem(checkGroup, checkitemIds);
    }


    //删除检查组功能

    @Override
    public void delete(Integer checkgroupId) {
        //1.删除中间表中该检查组关联的检查项数据
        checkGroupMapper.deleteCheckGroupAndCheckItem(checkgroupId);

        //2.删除检查组表中该检查组数据
        checkGroupMapper.delete(checkgroupId);
    }

    //查询所有检查组功能

    @Override
    public List<CheckGroup> findAll() {
        return checkGroupMapper.findAll();
    }
}
